package com.markyao.controller.recos;

import com.markyao.service.AuthService;
import com.markyao.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.ConcurrentHashMap;

@RestController
@RequestMapping("recos/group/")
public class RecosGroupController {

    @Autowired
    AuthService authService;

    @Autowired
    @Qualifier("leaderMetadata")
    ConcurrentHashMap<String,Object> leaderMap;

    @GetMapping("getLeaderAndToken")
    public Result bind(@RequestParam("username") String username,@RequestParam("password") String password){
        String token = authService.authing(username, password);
        if (token!=null){
            String host = leaderMap.get("host").toString();
            int port= (int) leaderMap.get("port");
            boolean alive = (boolean) leaderMap.get("alive");
            if (alive){
                String leaderUrl=host+":"+port;
                return Result.success(leaderUrl,token);
            }else {
                return Result.failed(500,"领导人暂时死亡..");
            }
        }
        return Result.failed(500,"验证失败");
    }
}
